<?php

namespace Bean\User;

class Request {

	private $_data;
	private $_requester;
	private $_acceptor;

	public function __construct( $data ) {
		$this->_data = $data;
	}
	
	public static function get( $request_id ) {
		$request = _q()->get1( 'users_requests', array( 'request_id' => $request_id ) );
		return new self( $request );
	}
	
	public function getRequester() {
		if( $this->_requester === null ) {
			$this->_requester = \Bean\User::get( $this->_data['requester_id'] );
		}
		return $this->_requester;
	}
	
	public function getAcceptor() {
		if( $this->_acceptor === null ) {
			$this->_acceptor = \Bean\User::get( $this->_data['acceptor_id'] );
		}
		return $this->_acceptor;
	}
	
	public function getMessage() {
		return $this->_data['message'];
	}
	
	public function getRequestId() {
		return $this->_data['request_id'];
	}
	
	public function delete() {
		_q()->del( 'users_requests', array( 'request_id' => $this->getRequestId() ) );
		return $this;
	}
	
	public function refuse() {
		return $this->delete();
	}
	
	public function accept() {
		_q()->upt( 'users_requests', 
			array( 'accepted' => 1 ),
			array( 'request_id' => $this->getRequestId() ) );
		return $this;
	}
}
